//------------------------------------------------------------------------
// <copyright file="Context.cs" company="DevDir">
//   Copyleft (c) DevDir. No rights reserved.
// </copyright>
// <author>Nikita Sharov</author>
//------------------------------------------------------------------------

namespace DevDir.Mp3.Equilibrist
{
  using System;
  using System.Diagnostics;
  using System.Windows.Forms;
  using System.Threading;
  using System.ComponentModel;
  using Microsoft.SqlServer.MessageBox;
  using DevDir.Mp3.Equilibrist.Forms;

  /// <summary>
  /// Main control of the application.
  /// </summary>
  public class Context : ApplicationContext
  {
    private SplashForm splashForm;
    
    /// <summary>
    /// Initializes a new instance of the <b>Context</b> class.
    /// </summary>
    public Context()
    {
      Application.ThreadException +=
        new ThreadExceptionEventHandler(ApplicationThreadException);
      Application.SetUnhandledExceptionMode(
        UnhandledExceptionMode.CatchException);
      AppDomain.CurrentDomain.UnhandledException +=
        new UnhandledExceptionEventHandler(
        CurrentDomainUnhandledException);

      this.splashForm = new SplashForm();
      this.splashForm.Show();

      this.splashForm.StatusLabel.Text = "Initialization...";
      this.splashForm.Refresh();

      MainController controller = new MainController();
      controller.MainForm = new MainForm();
      this.MainForm = controller.MainForm;
      
      this.splashForm.StatusLabel.Text += "done";
      this.splashForm.Refresh();

      this.splashForm.Close();
      this.MainForm.Show();
    }

    private void ApplicationThreadException(
      object sender, ThreadExceptionEventArgs e)
    {
      Exception ex = new Exception(
        "Unhandled ui thread exception, terminate?",
        e.Exception);
      ExceptionMessageBox b = 
        new ExceptionMessageBox(ex, ExceptionMessageBoxButtons.OKCancel);

      DialogResult result;
      if (this.MainForm != null)
      {
        result = b.Show(this.MainForm);
      }
      else
      {
        result = b.Show(this.splashForm);
      }
      
      if (result == DialogResult.OK)
      {
        Application.Exit();
      }
    }

    private void CurrentDomainUnhandledException(
      object sender, UnhandledExceptionEventArgs e)
    {      
      ExceptionMessageBox b;
      if (e.IsTerminating)
      {
        Exception ex = new Exception(
          "Unhandled exception, application is terminating.",
          (Exception)e.ExceptionObject);
        b = new ExceptionMessageBox(ex);
      }
      else
      {
        Exception ex = new Exception(
          "Unhandled exception, terminate?",
          (Exception)e.ExceptionObject);
        b = new ExceptionMessageBox(
          ex, ExceptionMessageBoxButtons.OKCancel);
      }

      DialogResult result;
      if (this.MainForm != null)
      {
        result = b.Show(this.MainForm);
      }
      else
      {
        result = b.Show(this.splashForm);
      }

      if (result == DialogResult.OK)
      {
        Application.Exit();
      }
    }
  }
}
